<?php
	if(isset($_POST['fieldid']))
	{
		$filedid = $_POST['fieldid'];
		GetCategoryByField($filedid);
	}
	if(isset($_GET['keysearch']))
	{
		$bookname = $mysqli->real_escape_string($_GET['bookname']);
		GetBookByAlphabet($bookname);
	}
?>
<?php
function GetCategoryByField($fieldID)
{
	require"../common/db.inc";
	$mysqli = new mysqli($host,$username,$password,$dbname);
	if(mysqli_connect_errno())
	{
		printf("Connect failed: %s\n",mysqli_connect_error());
		exit;
	}
	@$mysqli->query("set names 'utf8'");
	$sql_GetCategoryByField = "CALL sp_GetCategoryByField(?)";
	$stmt = $mysqli->prepare($sql_GetCategoryByField);
	if($mysqli->errno)
	{
		die($mysqli->errno."::".$mysqli->error);
	}
	$stmt->bind_param("i",$fieldID);
	$stmt->execute();
	if($mysqli->errno)
	{
		die("Execute failed:".$mysqli->errno.": ".$mysqli->error);
	}
	$stmt->bind_result($catID,$catName,$field);
	$catid = 0;
	if(isset($_POST['catid']))
		$catid = $_POST['catid'];
	print('<select name="CategoryID" id="CategoryID">');
	printf("<option value='0' selected='true'>Chọn danh mục</option>");
	while($stmt->fetch())
	{
		if($catid == $catID)
		{
			printf("<option value='%d' selected='true'>%s</option>",$catID,$catName);
		}
		else
			printf("<option value='%d'>%s</option>",$catID,$catName);
	}
	print('</select>');
	$mysqli->close();
}

function GetBookByAlphabet($bookname)
{
	require"../common/db.inc";
	$mysqli = new mysqli($host,$username,$password,$dbname);
	$mysqli_count = new mysqli($host,$username,$password,$dbname);
	if(mysqli_connect_errno())
	{
		printf("Connect failed: %s\n",mysqli_connect_error());
		exit;
	}
	@$mysqli->query("set names 'utf8'");
	
	$books_per_page = 12;
	$numdisplay = 5;
	$current_page = 1;
	if(isset($_GET['page']))
		$current_page = $mysqli->real_escape_string($_GET['page']);
	$start = $books_per_page * ($current_page - 1);
	$limit = $books_per_page;
		
	$sql_GetBookByKey = "CALL sp_GetEbookByAlphabet('$bookname',$start,$limit)";
	$sql_CountGetBookByKey = "CALL sp_CountGetEbookByAlphabet('$bookname')";
	
	@$result = $mysqli->query($sql_GetBookByKey);
	@$result_count = $mysqli_count->query($sql_CountGetBookByKey);
	
	if($mysqli->errno)
	{
		die($mysqli->errno."::".$mysqli->error);
	}
	
	if($result->num_rows > 0)
	{
		print("<br><table width='100%' cellspacing='6'>");
		while($row = $result->fetch_object())
		{
			print("<tr>");
			printf("<td valign='top' align='center' width='%d'><a href='%s'><img src='%s' height='150' width='100' /></a><br/><a href='%s'>%s</a></td>",25, 'XemChiTietSach.php?id='.$row->ID,$image.$row->CoverImage,'XemChiTietSach.php?id='.$row->ID,$row->Title);
			$i = 0;
			while($i < 3 && $row2 = $result->fetch_object())
			{
				printf("<td valign='top' align='center' width='%d'><a href='%s'><img src='%s' height='150' width='100' /></a><br/><a href='%s'>%s</a></td>",25, 'XemChiTietSach.php?id='.$row2->ID,$image.$row2->CoverImage,'XemChiTietSach.php?id='.$row2->ID,$row2->Title);
				$i++;
			}
			print("</tr>");
		}
		print("</table><br/><br/>");
		
		$num_rows = 0;
		while($row = $result_count->fetch_object())
		{
			$num_rows = $row->count_result;
		}
		
		$numPages = ceil($num_rows/$books_per_page);
		$offset = ($current_page-1)*$books_per_page;
		$haft_limit = floor($numdisplay/2);
		
		if ($numPages > 1) 
		{ 
			$lowLimit = 0;
			$hiLimit = 0;
			
			if($current_page - $haft_limit < 1)
			{
				$lowLimit = 1;
				$highLimit = $numPages < $numdisplay ? $numPages : ($lowLimit + ($numdisplay-1));
			}
			else
			{
				$highLimit = $current_page+$haft_limit < $numPages ? $current_page+$haft_limit : $numPages;
				$lowLimit = $highLimit - ($numdisplay-1);
				if($lowLimit < 1)
					$lowLimit = 1;
			}			
			
			
			if($current_page > 1)
			{
				printf("<span class='nav'><a href='#' onclick='".'GetBookByKey("%s",%d); return false;'."'><b>Đầu</b></a></span>",$bookname,1); 
				$prev = $current_page - 1;
				printf("<span class='nav'><a href='#' onclick='".'GetBookByKey("%s",%d); return false;'."'><b>Trước</b></a></span>",$bookname,$prev); 
			}
			else
			{
				printf("<span class='nav'>Đầu</span>");
				printf("<span class='nav'>Trước</span>"); 
			}
			
			for ( $i = $lowLimit; $i <= $highLimit; $i++ ) 
			{ 
				if($i == $current_page)	//trang đang chọn
					printf("<span class='active nav'><b>{$current_page}</b></span>"); 				
				else
					printf("<span class='nav'><a href='#' onclick='".'GetBookByKey("%s",%d); return false'."'>$i</a></span>",$bookname,$i); 				
			}
			
			if($current_page < $numPages)
			{
				$next = $current_page + 1;
				printf("<span class='nav'><a href='#' onclick='".'GetBookByKey("%s",%d); return false'."'><b>Tiếp</b></a></span>",$bookname,$next); 
				printf("<span class='nav'><a href='#' onclick='".'GetBookByKey("%s",%d); return false'."'><b>Cuối</b></a></span>",$bookname,$numPages); 
			}
			else
			{
				printf("<span class='nav'>Tiếp</span>"); 
				printf("<span class='nav'>Cuối</span>"); 		
			}		
		}  
	}
	else
		print("<center><font color='red' size='3'>Không có sách thỏa từ khóa bạn đang tìm</font></center>");
	$mysqli->close();
}
?>